Repository navigation
fix(zcode): launch hooks via hidden wscript VBS — no console flash, bounded wait - #596
Conversation
…on-blocking Windows console children (cmd / bash) allocate a visible console window when spawned by the desktop app, so every hook run flashed a black box; and inline network pulls could exceed short timeouts. ZCode entries now launch through wscript.exe (GUI subsystem, present in System32) running a teamai-hook-dispatch.vbs written next to config.json: the VBS runs the dispatch hidden (window style 0) and does not wait for it, so session start stays instant. The payload travels verbatim as a single argument, preserving the manifest command invariant. POSIX entries keep bash -lc.
jeff-r2026
left a comment
There was a problem hiding this comment.
Thanks for chasing the console flash — wscript.exe is the right primitive for a GUI-subsystem launcher on Windows. CI is green and the branch is mergeable, but as it stands I don't think this can land: the implementation is narrower than the PR description in one place and wider in another.
The two blockers
- The
process.platform === 'win32'branch is gone, sowscript.exeis written on every platform. The description says POSIX keepsbash -lc; the code doesn't. Existing macOS/Linux entries get replaced on the next reconcile. - The VBS launcher drops STDIN and makes all four events fire-and-forget, which breaks the payload contract
hook-dispatchrelies on and removes the effect of the per-eventtimeoutMstable. Details inline.
Smaller things worth folding in
removeAllnever deletesteamai-hook-dispatch.vbs, so uninstall leaves it behind.- The
zcodeEntryCommanddoc comment ("Both variants (posix bash -lc / win32 cmd /c)") no longer describes the code. - Behavior change isn't reflected in the docs.
docs/usage-guide.mdstill says hook entries use "bash -lc <dispatch>as an argv vector", and the zh-CN guide has the matching sentence. Per the repo rules both need updating in the same PR.
Suggested validation after the rework
On Windows: SessionStart auto-pull, plus a PostToolUse event with a Skill / TodoWrite matcher to confirm tool_name actually arrives. On macOS or Linux: confirm the written entry is still bash -lc and that reconcile stays idempotent across the platform change.
| // managed-entry detection and the manifest keep one command representation. | ||
| const entry: ZcodeHookEntry = { | ||
| type: 'process', | ||
| command: 'wscript.exe', |
There was a problem hiding this comment.
This writes wscript.exe unconditionally — the process.platform === 'win32' branch that used to pick cmd vs bash is gone, which contradicts the "POSIX (macOS/Linux) entries keep bash -lc" line in the PR description.
On macOS/Linux the next reconcile will swap the existing bash -lc entries for this one: the payload still lives in args[1], so isManaged recognizes them as managed and rewrites them. ZCode then tries to spawn a launcher that doesn't exist on those platforms and every hook stops firing.
The Ubuntu and macOS CI jobs don't catch this because the test was changed to assert wscript.exe on all platforms too. Could you restore the platform branch and keep wscript.exe scoped to win32?
| [ | ||
| "' TeamAI hook dispatcher - hidden, fire-and-forget (no console window).", | ||
| 'Set sh = CreateObject("WScript.Shell")', | ||
| 'If WScript.Arguments.Count > 0 Then sh.Run "cmd /c " & WScript.Arguments(0), 0, False', |
There was a problem hiding this comment.
Three concerns with this line.
STDIN is dropped. WScript.Shell.Run starts a fresh process with no inherited stdin pipe, so whatever ZCode writes to wscript.exe never reaches teamai. hook-dispatch reads its payload from STDIN JSON (cwd, tool_name, tool_input, prompt). SessionStart may still look fine because the pull handler can fall back to the child's cwd — which matches what the dogfood run observed — but PostToolUse track / todowrite-hint no-op without tool_name, UserPromptSubmit loses prompt, and any Stop-time hint can't reach the host. Spooling the payload to a temp file and redirecting it into the command would keep the contract intact.
Wait=False is applied too broadly. All four events become fire-and-forget, not just SessionStart. The pull handler is already registered with background: true in the dispatcher, so it doesn't need this; meanwhile the ZCODE_TIMEOUT_MS table above (SessionStart 180s, Stop 60s, …) no longer bounds anything, since the launcher returns immediately regardless.
The payload isn't escaped. "cmd /c " & WScript.Arguments(0) concatenates the command straight into cmd. Built-in dispatch commands survive it, but team hooks are arbitrary strings — the test suite itself uses sh /tmp/audit.sh — and one &, >, or embedded quote gets re-split by cmd. Arguments(0) needs cmd-level quoting.
| const expanded = expandHome(settingsPath); | ||
| await ensureDir(path.dirname(expanded)); | ||
| const vbsPath = path.join(path.dirname(expanded), 'teamai-hook-dispatch.vbs'); | ||
| if (!opts.removeAll) { |
There was a problem hiding this comment.
Two minor points on the write itself:
- The
!opts.removeAllguard means uninstall skips the write, but nothing ever deletesteamai-hook-dispatch.vbs—removeAllshould remove it so teardown leaves no stray file next toconfig.json. - This rewrites the VBS on every reconcile, even when the content is identical. The rest of this function is careful to only write on change (the
changedflag); worth matching that so a no-op sync doesn't touch the file's mtime.
| // wscript.exe is a GUI-subsystem binary — hook runs never flash a | ||
| // console window, and the hidden VBS launcher keeps the session | ||
| // start non-blocking even while the dispatch pulls over the network. | ||
| expect(hook.command).toBe('wscript.exe'); |
There was a problem hiding this comment.
With the platform conditional removed here, the macOS and Ubuntu CI jobs no longer assert anything about the POSIX entry shape — which is why the regression above passes CI. If the win32 branch comes back in toZcodeEntry, this assertion should branch with it.
The PR's Validation section also mentions a "VBS launcher existence" assertion, but I don't see one in the diff; a check that teamai-hook-dispatch.vbs is written next to config.json (and removed by removeAll) would be a good addition.
…t launcher Aligns the test assertions and zcodeEntryCommand extraction with the wscript launcher argv (vbsPath, mode, payload). The explicitly configured timeout (team hooks.yaml / builtin.overrides) now wins over the per-event table, and the table is the per-event default otherwise.
Review — hook launch mechanism looks broken as writtenThanks for tackling the console-flash — the direction (GUI-subsystem launcher, spooled stdin, per-event timeouts) is sound. But reading the PR head, I think the argv ↔ VBS contract is mismatched and ZCode hooks won't actually dispatch. Details below, ordered by severity. 🔴 P0 — VBS launcher and argv contract are mismatched; dispatch never runs correctly
But the VBS reads sh.Run "cmd /d /s /c ""teamai hook-dispatch " & WScript.Arguments(0) & " < """ & spool & """ >nul 2>&1""", 0, TrueUnder
So every hook run actually executes: i.e. event = Note that simply bumping the index to Fix (pick one, keep both sides aligned):
Also, the 🟠 P1 — VBS file leaks on uninstall (
|
…ent timeouts, VBS lifecycle Review round on Tencent#596 (jeff-r2026), all points addressed: - P0: the VBS launcher consumed Arguments(0) (the mode slot 'wait') as the dispatch event, so hooks dispatched 'teamai hook-dispatch wait' — an invalid event. The entry now passes [vbsPath, tail] and the VBS uses Arguments(1) (the raw dispatch command) directly — no hardcoded prefix, no mode arg, no double prefix. - P1: removeAll now deletes teamai-hook-dispatch.vbs unconditionally (previously gated on content-differs, leaving an orphan on uninstall). - The VBS spools ZCode's stdin payload to a temp file and feeds it to the dispatch via input redirection, preserving the STDIN contract for PostToolUse/UserPromptSubmit; per-event timeoutMs bounds the launch. - POSIX entries keep bash -lc verbatim (platform branch restored — the unconditional wscript would have broken macOS/Linux on the next sync). - Tests: payload asserted at args[2]; matcher-flag detection moved with it. Validation (real box): two consecutive injects → still exactly 6 entries, no duplicates; hidden launch verified end-to-end (session-start auto-pull completed through the VBS chain).
…last-arg command extraction The 961c1e3 template kept the hardcoded 'teamai hook-dispatch ' prefix in the cmd line while entries already pass the full dispatch tail, doubling the prefix: cmd split the tail into 'event=teamai' and every dispatch failed (observed live: 'failed to parse STDIN JSON for event=teamai', hook.run.failed on each run). The VBS now builds the cmd line from Arguments(0) alone. zcodeEntryCommand still assumed the d678c8d three-slot argv ([vbs, mode, tail]) and returned hook.command ('wscript.exe') for the reverted two-slot shape, so (a) entries written by the current code failed their own managed-entry check and (b) legacy mode-slot entries were never recognized for replacement — a reconcile appended duplicates alongside the orphans. The command tail is always the LAST argv slot in both generations; reading the last slot also restores team-command matching against the managed-hooks manifest (no marker in team commands). Verified end-to-end on Windows: reconcile replaced the 12 mixed-era entries with the 6 current ones, and a piped payload through cscript + VBS + cmd reached hook-dispatch with the correct event (recorded in the dashboard).
… Windows launcher
Real-box verification of the VBS launcher found the ANSI-codepage round
trip breaks UTF-8 payloads containing multi-byte text at the first
non-ASCII sequence (quote swallowed / chars written as '?'), leaving
JSON.parse to fail. The degraded path already dispatches with {} and
exits 0, but the anonymous event lost its session and tool linkage.
parseStdin now regex-salvages the intact ASCII head — session_id,
tool_name, transcript_path, tool_use_id, cwd (snake_case and camelCase
variants; camelCase hits are mirrored onto the canonical snake_case
names handlers read) — so a degraded dispatch still lands on the right
session and tool in the dashboard. Verified end-to-end: a mangled
payload records 'tool_use for session <id> [tool=Bash]' instead of the
pid fallback.
Long-term fix tracked for the ZCode integration: the hook runner
spawning hook commands with windowsHide would let console commands
forward STDIN byte-exact and retire the launcher spool entirely.
|
Pushed two commits from real-box verification of the launcher — thanks again for the round-2 review; both P0s you called out were real.
One upstream thought: if the ZCode hook runner spawned hook commands with Verified end-to-end on Windows: reconcile replaced the mixed-era entries with exactly the current set; clean and mangled payloads piped through the launcher reach hook-dispatch with correct events and identity. |
Conflict resolution: upstream added the Copilot adapter in the same region as toZcodeEntry. Copilot functions are kept; toZcodeEntry keeps the PR's launcher signature (vbsPath) — upstream's interim direct- command shape is what this PR replaces.
|
P0s from last round look fixed on
Nit: assert the VBS uses |
… docs Review round 3 on Tencent#596 (jeff-r2026): - removeAll now deletes teamai-hook-dispatch.vbs unconditionally: the delete previously sat inside a content-diff gate, so after a normal inject (file == template) the removal never fired and the script was left behind. Uses fs/promises rm instead of awaited rmSync. - toZcodeEntry restores the win32/POSIX branch: injection is gated only on ~/.zcode existing, so the unconditional wscript entry replaced POSIX users' bash -lc entries. POSIX renders bash -lc like the other shell formats; the launcher script is only written on win32. The command tail stays the last argv slot on both branches, so zcodeEntryCommand's last-arg extraction covers both. - usage-guide.md / usage-guide.zh-CN.md describe the actual entry shapes (VBS launcher on Windows, bash -lc on POSIX) instead of the stale bash-only wording. - Tests: VBS content assertions (Arguments(0), no hardcoded dispatch prefix), launcher-gone assertion after removeAll, and platform-conditional shape assertions so the POSIX branch is covered on Linux CI.
…awn escape Both additions landed in the same region of hook-dispatch-cli.ts and are independent: the salvage covers mangled STDIN payloads at parse time; Tencent#608's WMI path covers detached children killed by Windows job objects. No behavior overlap.
|
All three points plus the nit are addressed in
Nit: the win32 shape test now reads the written VBS and asserts it dispatches One note on the merge: #608's WMI escape landed in the same region of |
|
Findings
Testing
|
Automated review finding: the zcode branch of getHookStatus reported 'installed' from config.json alone, so a deleted, stale, or AV- quarantined teamai-hook-dispatch.vbs left dead hooks reported as healthy. The launcher file is now required on win32 (POSIX entries are plain bash and have no launcher).
|
Responding to the automated review findings (all three checked against the real-box instrumentation from the earlier rounds): [P1] "Windows hooks cannot receive STDIN — empty spool" — false positive. [P1] "The launcher is blocking" — accurate about the mechanism, intended by design. [P2] "status ignores the launcher file" — valid, fixed in |
…ounded wait (Tencent#596) Windows ZCode hook entries launch through wscript.exe running a hidden teamai-hook-dispatch.vbs written next to config.json, eliminating the console-window flash on every hook run. The launcher spools STDIN to a temp file to preserve the payload contract and runs the dispatch with a per-event bounded wait (SessionStart 180s, Stop/UserPromptSubmit 60s, PostToolUse 30s). POSIX entries keep bash -lc. Identity fields are salvaged when a multi-byte payload degrades at the ANSI-codepage spool step, and getHookStatus requires the launcher on win32.
Summary
wscript.exe(GUI subsystem) running a smallteamai-hook-dispatch.vbswritten next toconfig.json— the VBS executes the dispatchhidden (window style 0) and fire-and-forget
never block the session
Context: why cmd / bash entries are problematic for ZCode
ZCode desktop (Windows) spawns
process-type hook entries directly:cmd /c "..."allocates a visible console window when spawned by the desktop app — everyhook run flashed a black box over the desktop (reported by our team during dogfooding)
bashis not safe either: CreateProcess resolves it to System32's WSL launcher beforeany PATH directory, and the WSL side has a different
$HOME(no teamai state) and often noNode ≥ 20 — the hook then no-ops or dies silently
wscript.exeis a GUI-subsystem binary that is always present, so it is the only reliablelauncher on Windows
Design
type/command/args/timeoutMs); the payloadtravels verbatim as a single argument (
args[1]), so managed-entry detection and themanaged-hooks manifest keep the one-command-representation invariant introduced for team hooks
config.json(one per scope) and is idempotent —re-injection does not duplicate it
SessionStart180s, others 30–60s) still bound the runnerbash -lc; there is no console-window concept therefollow-up once its storage/format is documented
Validation
npx vitest run src/__tests__/zcode.test.ts— 11/11 passed (shape assertions updated:wscript.exe+ payload arg, VBS launcher existence, matcher rules)npm run typecheck,npm run buildpasshooks inject, session-start auto-pull completedthrough the hidden launcher (
teamai statuslast-pull refreshed), zerohook.run.failedentries for the wscript entries, second sync byte-identical (idempotent)
Boundary
wscript.exeships with Windows; POSIX keepsbash)storage/format is documented
Type of Change
Test Plan
npx tsc --noEmitpassesnpx vitest runpassesRelated Issues
Windows desktop